// pages/my/myFactory/myFactory.js
const app = getApp()
Page({
  data: {
    accountId:'',
    factoryList:[],
    isMask:false
  },
  onLoad: function (options) {
    // app.getMemInfo().then(res=>{
    //   this.setData({
    //     accountId:res.data.id
    //   })
    //   this.getFactoryList(res.data.id)
    // })
  },
  onShow: function () {
    app.getMemInfo().then(res=>{
      this.setData({
        accountId:res.data.id
      })
      this.getFactoryList(res.data.id)
    })
  },
  getFactoryList(accountId){
    let data = {
      accountId
    }
    console.log(data)
    app.request('wx_app/factory/list',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let factoryList = []
        res.data.forEach(item=>{
          // if (item.id!=1) {
            factoryList.push({
              factoryAddress:item.factoryAddress,
              company_name:item.company_name,
              factory_phone:item.tel_phone,
              mobile_phone:item.mobile_phone,
              url:item.logo_url,
              id:item.id,
              addressId:item.myAddress.addressId,
              contactName:item.myAddress.contactName,
              contactAddress:item.myAddress.address,
              contactPhone:item.myAddress.phone
            })
          // }
        })
        this.setData({
          factoryList
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  openInvitation(){
    this.setData({
      isMask:true
    })
  },
  bindCancel(){
    this.setData({
      isMask:false
    })
  },
  getInviteCode(e){
    this.setData({
      inviteCode:e.detail.value
    })
  },
  bindConfirm(){
    let that = this
    let data = {
      accountId:this.data.accountId,
      inviteCode:this.data.inviteCode
    }
    app.request('wx_app/agent_account/input_inviteCode',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        this.getFactoryList(this.data.accountId)
        this.setData({
          isMask:false
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  bindCall(e){
    let phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber:phone,
      success(){
        console.log('呼叫成功')
      },
      fail(){
        console.log('呼叫失败')
      }
    })
  },
  editMyFactory(e){
    wx.navigateTo({
      url:'editMyFactory?id='+e.currentTarget.dataset.id
    })
  }
})